//
//  ZLStatusCell.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 16/12/27.
//  Copyright © 2016年 hanju001. All rights reserved.
//

import UIKit

/// 微博cell 的协议
//可选协议的时候添加@objc 需要遵守NSObjectProtocol
@objc protocol ZLStatusCellDelegate: NSObjectProtocol {
    
    @objc optional func statusCellDidTapURLString(cell: ZLStatusCell, urlString: String)
    
}

class ZLStatusCell: UITableViewCell {

    weak var delegate: ZLStatusCellDelegate?

    var vieModel: ZLStatusViewModel? {
        
        didSet{
            
            statusLabel?.attributedText = vieModel?.statusAttrText
            
            nameLabel.text = vieModel?.status.user?.screen_name
            
            meberIconView.image = vieModel?.memberIcon
            
            vipIconView.image = vieModel?.vipicon
            
            iconView.hm_setImage(urlString: (vieModel?.status.user?.profile_image_url), placeholder: UIImage(named: "avatar_default_big"), isAvatar: true)
            
            toolbar.viewModel = vieModel
            
            //修改配图视图模型
            pictureView.Viewmodel = vieModel
           // pictureView.heightCons.constant = vieModel?.pictureViewSize.height ?? 0;
            if vieModel?.pictureViewSize.height == 0{
                
                topPicHeight.constant = 0
            }else{
                topPicHeight.constant = 12
            }
            
            //测试4张图像
            /*if (vieModel?.status.pic_urls?.count)! > 4{
                
                var picURLS = vieModel?.status.pic_urls
                picURLS?.removeSubrange((picURLS?.startIndex)! + 4..<(picURLS?.endIndex)!)
                pictureView.urls = picURLS
                
            }
            else{
                pictureView.urls = vieModel?.status.pic_urls
            }*/
            
            //设置被转发微博的文字
            retweetedLabel?.attributedText = vieModel?.retweetedAttrText
            
            timeLabel.text = vieModel?.status.createDate?.zl_dateDescription
            
            sourceLabel.text = vieModel?.status.source
        }
    }
    
    /// 头像
    @IBOutlet weak var iconView: UIImageView!
    //姓名
    @IBOutlet weak var nameLabel: UILabel!
    //会员
    @IBOutlet weak var meberIconView: UIImageView!
    /// 时间
    @IBOutlet weak var timeLabel: UILabel!
    /// 来源
    @IBOutlet weak var sourceLabel: UILabel!
    
    /// 微博
    @IBOutlet weak var statusLabel: FFLabel!
    
    /// vip图标
    @IBOutlet weak var vipIconView: UIImageView!
    //底部工具栏
    @IBOutlet weak var toolbar: ZLStatusToolbar!
    //配图视图
    @IBOutlet weak var pictureView: ZLStatusPictureView!
    //配图视图顶部的的约束
    @IBOutlet weak var topPicHeight: NSLayoutConstraint!
    //被转发微博的标签 原创微博没有这个控件  一定要用‘？’
    @IBOutlet weak var retweetedLabel: FFLabel?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        //离屏渲染
        self.layer.drawsAsynchronously = true
        //格栅华- 异步绘制之后会生成一张独立的图像，cell滚动的时候就是滚动的这张头像
        //cell相当于只有一层头像
        //停止滚动之后可以监听用户交互
        self.layer.shouldRasterize = true
        //栅格化  必须制定分辨率
        self.layer.rasterizationScale = UIScreen.main.scale
        //设置微博文本代理
        statusLabel.delegate = self;
        retweetedLabel?.delegate = self;
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
extension ZLStatusCell: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        //判断是否是url
        if !text.hasPrefix("http://"){
            return
        }
        //插入？表示如果代理没有实现协议方法就什么都不做
        //如果使用！ 代理没有实现协议方法 仍然强行执行就会崩溃
        delegate?.statusCellDidTapURLString?(cell: self, urlString: text)
    }
}
